package com.lb.jwitter.action;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienPhieuDangKyMonHocAddFormAction{
    private int id; 
    public void setId(int id) {
       this.id = id;
 }

    public int getId() {
       return id;
 }

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private List<TblCanBo> tblCanBos; 
    public List<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private TblCanBoService tblCanBoService; 
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam; 
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 
    
    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 
    
    private int hocKyId;
    private Date today;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    
//    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "them")){ 
//        return "noprivilege"; 
//    }        
    tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        
        tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findAlltblQLDTTXAHocVien(0, 0);

        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 

        today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(hocKyId != 0){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1(new Long(hocKyId));
            con.setOperator("eq");
            cons.add(con);	
        } else {
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
        	
        	
        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        cons.clear();
        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
        	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        	
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNams.get(0).getId());
            con.setOperator("eq");
            cons.add(con);	
        }
        List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
        
        tblQLDTTXAMonHocs = new ArrayList<TblQLDTTXAMonHoc>();
        List<Long> monHocIds = new ArrayList<Long>();
        for(TblQLDTTXALopMonHocDuKien tblLopDuKien : tblQLDTTXALopMonHocDuKiens){
     	   if(!monHocIds.contains(tblLopDuKien.getMonHocId().getId())){
    		   tblQLDTTXAMonHocs.add(tblLopDuKien.getMonHocId());
    		   monHocIds.add(tblLopDuKien.getMonHocId().getId());
    	   }
        	
        }
        
        cons.clear();
        
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);
        
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi AddForm tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public int getHocKyId() {
	return hocKyId;
}

public void setHocKyId(int hocKyId) {
	this.hocKyId = hocKyId;
}

public Date getToday() {
	return today;
}

public void setToday(Date today) {
	this.today = today;
}

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
} 
 } 
 
